package com.jlnk.picassodemo.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jlnk.picassodemo.entity.ImageEntity;
import com.jlnk.picassodemo.R;
import com.jlnk.picassodemo.views.ImageActivity;
import com.squareup.picasso.Picasso;

import java.util.List;

public class CustomArrayAdapter extends ArrayAdapter<ImageEntity> {
    private final List<ImageEntity> imageList;

    public CustomArrayAdapter(Context context, List<ImageEntity> imageList) {
        super(context, R.layout.list_item, imageList);
        this.imageList = imageList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
        }

        ImageView thumbImageView = convertView.findViewById(R.id.thumb_image_view);
        TextView titleTextView = convertView.findViewById(R.id.title_text_view);
        ImageEntity image = imageList.get(position);

        titleTextView.setText(image.getTitle());
        Picasso.get().load(image.getThumbUrl()).into(thumbImageView);

        titleTextView.setOnClickListener(v -> {
            Intent intent = new Intent(getContext(), ImageActivity.class);
            intent.putExtra("imageUrl", image.getFullUrl());
            getContext().startActivity(intent);
        });
        return convertView;
    }
}